from manimlib import *
from numpy import flip, insert

LV,QL,QH,JH='#75cfb8','#bbdfc8','#f0e5d8','#ffc478'

class p1(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        axes = Axes(
            x_range=[-1, 4, 1],
            y_range=[-1.5, 1.5, 1],
            x_length=10,
            y_length=6,
            x_axis_config={
                "numbers_to_include": np.arange(-1, 4, 1),
            },

            tips=False,
        ).set_color(BLACK)
        for i in range(3):
            f=axes.plot(lambda x: 2**i*x*(x-1), x_range=[0,1]).shift(RIGHT*i*2)
            self.add(f,
                DashedLine(ORIGIN,RIGHT*((1+2*i))).shift(LEFT*3+DOWN*0.5*2**i)
            )
        s=0.7
        self.add(axes,
            DashedLine(ORIGIN,RIGHT*14/3).shift(LEFT*3+DOWN*16/9).set_color(JH),
            DashedLine(ORIGIN,DOWN*16/9).shift(RIGHT*5/3).set_color(JH),
            Tex('x_1').move_to([5/3,0.2,0]),
            Tex(r'-\frac{1}{4}').shift(LEFT*3.3+DOWN/2).scale(s),
            Tex(r'-\frac{1}{2}').shift(LEFT*3.3+DOWN).scale(s),
            Tex(r'-\frac{8}{9}').shift(LEFT*3.3+DOWN*16/9).scale(s),
            Tex(r'-1').shift(LEFT*3.3+DOWN*2.1).scale(s),
        )
        self.wait()

class p1(Scene):
    def construct(self):
        # self.camera.frame.save_state()
        # self.add(NumberPlane())
        # self.camera.frame.rotate(13*DEGREES,about_point=[0.0, 0.0, 1.0])
        NumberLine
        axes = Axes(
            x_range=[-2.5, 3.5, 1],
            y_range=[-1.5, 0.5, 1],
            x_length=7.9,
            y_length=4,
            x_axis_config={
                "numbers_to_include": np.arange(-2, 4, 1),
            },
            y_axis_config={"numbers_to_include":[-1]},
            tips=True,
        ).set_color(BLACK)
        # graph
        f1=axes.plot(lambda x:x*(x-1), x_range=[0,1]).set_color(LV)
        f2=axes.plot(lambda x:2*(x-1)*(x-2), x_range=[1,2]).set_color(LV)
        f3=axes.plot(lambda x:4*(x-2)*(x-3), x_range=[2,3]).set_color(LV)
        f4=axes.plot(lambda x:x*(x+1)/2, x_range=[-1,0]).set_color(LV)
        f5=axes.plot(lambda x:(x+2)*(x+1)/2, x_range=[-2,-1]).set_color(LV)

        self.add(axes,f1,f2,f3,f4,f5,
            Tex('x(x-1)').next_to(f1,DOWN),
            Tex('2(x-1)(x-2)').next_to(f2,DOWN),
            Tex('4(x-2)(x-3)').next_to(f3,DOWN).shift(UP*0.5),
            Line(LEFT*2,RIGHT*4).shift(DOWN*8/9).set_color(JH),
            Tex(r'f(x)< -\frac{8}{9}').next_to(f2,DOWN,buff=1.5),
            Arrow([2,-1.8,0],[2.5,-1,0],buff=0)
        )
        self.wait()